<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="_session_check.jsp" %>
<%@ page import="com.shopping.modean.*"%>
<%@ page import="com.shopping.service.*"%>
<%@ page import="com.shopping.util.*"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>

<%
	request.setCharacterEncoding("UTF-8");
	String pageNo = request.getParameter("pageNo");
	String pageSize = request.getParameter("pageSize");
	int pageNoValue = 1;
	int pageSizeValue = 3;
	if(pageNo != null) {
		pageNoValue = Integer.valueOf(pageNo);
		pageSizeValue = Integer.valueOf(pageSize);
	}
	List<Product> products = ProductService.getInstance().getProducts(pageNoValue,pageSizeValue);
	int totalCount = ProductService.getInstance().getTotalCount(pageSizeValue);
	int totalPage = totalCount%pageSizeValue == 0 ? totalCount/pageSizeValue : (totalCount/pageSizeValue + 1);
%>

<!DOCTYPE html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品列表</title>

<style type="text/css">
 * {
 	margin: 0;
 	padding: 0;
 }

</style>

<script type="text/javascript">
	function showDialog() {
		var dialog = document.getElementById("dialogDiv");
		dialog.style.display = "block";
	}
	
	function hideDialog() {
		var dialog = document.getElementById("dialogDiv");
		dialog.style.display = "none";
		clear();
	}
	
	function clear() {
		document.getElementById("id").value = "";
		document.getElementById("descr").value = "";
		document.getElementById("normal_price").value = "";
		document.getElementById("member_price").value = "";
		document.getElementById("category_id").value = "";
	}
	
	
	
	function update(id,descr,normalPrice,memberPrice,categoryId) {
		document.getElementById("id").value = id;
		document.getElementById("descr").value = descr;
		document.getElementById("normal_price").value = normalPrice;
		document.getElementById("member_price").value = memberPrice;
		document.getElementById("category_id").value = categoryId;
		showDialog();
		return false;
	}
	

</script>

</head>

<body>
  
  <div>
    <div style="width:783px;margin: 15px auto;">
		<input style="height: 30px;padding-left: 5px;" type="text" placeholder="输入商品描述"/>
		<button style="height: 34px;" onclick="">&nbsp;搜&nbsp;索&nbsp;</button>   
		<button style="height: 34px;" onclick="showDialog()">&nbsp;添&nbsp;加&nbsp;</button>  
    </div>
  	<table border="1" style="border-collapse:collapse;text-align: center;margin: 0 auto;">
	  	<tr height="60px">
	  		<!-- 775px -->
	  		<td width="50px">ID</td>
	  		<td width="100px">商品描述</td>
	  		<td width="80px">普通价格</td>
	  		<td width="80px">会员价格</td>
	  		<td width="175px">上架时间</td>
	  		<td width="80px">商品分类</td>
	  		<td width="210px">操作</td>
	  	</tr>
	    
	    <%
	    	for(Product p : products) {
	    %>
	    <tr height="60px">
	  		<td><%=p.getId() %></td>
	  		<td><%=p.getDescr() %></td>
	  		<td><%=p.getNormalPrice() %></td>
	  		<td><%=p.getMemberPrice() %></td>
	  		<td><%=TimeUtil.getStringFromTime(p.getPdate(),"yyyy-MM-dd HH:mm:ss") %></td>
	  		<td align="center"><%=p.getCategoryId() %></td>
	  		<td>
	  		  <!-- 
	  		     javascript是伪协议，表示url的内容通过javascript执行。
	  		     void(0)表示不作任何操作，防止链接跳转到其他页面。
	  		            让页面不跳转，javascript:void(0)是最通用方式。
	  		   -->
	  		  <a href="javascript:void(0)" onclick="update(<%=p.getId() %>,'<%=p.getDescr() %>','<%=p.getNormalPrice() %>','<%=p.getMemberPrice() %>',<%=p.getCategoryId() %>)">修改</a>
	  		  <span>|</span>
	  		  <a href="#">删除</a>
	  		  <span>|</span>
	  		  <a href="#">下架</a>
	  		</td>
	  	</tr>
	     <% } %>
	 </table>
	 
	 <div style="width:783px;margin: 15px auto;">
	    <div style="text-align: center;">
	    	<a href="product_list.jsp?pageNo=<%=pageNoValue-1 %>&pageSize=3" style="visibility: <%= pageNoValue==1 ? "hidden":"visible"%>">上一页</a>
			<span>第<%=pageNoValue %>页</span>
			<a href="product_list.jsp?pageNo=<%=pageNoValue+1 %>&pageSize=3" style="visibility: <%= pageNoValue==totalPage ? "hidden":"visible"%>">下一页</a>
			<span>共<%=totalPage %>页</span>
			<span><%=totalCount %>条</span>
	    </div>
    </div>
    
  </div>
  
  <div id = "dialogDiv" style="background-color:rgba(63, 63, 63, 0.7);display: none;width:100vw;height:100vh;position: fixed;top:0;left:0;">
	<div>
		<form method="post" id="product_add" name="product_add" action="product_add.jsp" 
		      style="background-color: #ffffff;position: absolute;left:0;top:0;right:0;bottom:0;width:800px;height: 500px;margin: auto; " >
		    <input type="hidden" name="action" value="productAdd" />
		    <input type="hidden" id="id" name="id" />
			<table border="1" class="tableborder" align="center" cellpadding="4" cellspacing="1" width="97%">
	
		        <tr>
		          <td class="altbg1">商品描述:</td>
		          <td class="altbg2">
		            <input id="descr" name="descr" size="25" maxlength="25" type="text"> 
		            <span id="usermsg"></span>
		          </td>
		        </tr>
				
		        <tr>
		          <td class="altbg1" valign="top">普通价格:</td>
		         <td class="altbg2">
		            <input id="normal_price" name="normal_price" size="25" maxlength="25" type="text"> 
		            <span id="usermsg"></span>
		          </td>
		        </tr>
		        
		        <tr>
		          <td class="altbg1" valign="top">会员价格:</td>
		         <td class="altbg2">
		            <input id="member_price" name="member_price" size="25" maxlength="25" type="text"> 
		            <span id="usermsg"></span>
		          </td>
		        </tr>
		        
		        <tr>
		          <td class="altbg1" valign="top">分类:</td>
		          <td class="altbg2">
		            <input id="category_id" name="category_id" size="25" maxlength="25" type="text"> 
		            <span id="usermsg"></span>
		          </td>
		        </tr>
		        
		        <tr>
		          <td class="altbg1"></td>
		          <td class="altbg2">
		          	<input name="regsubmit" value="确 &nbsp; 定" type="submit">
		          	<input name="cancel" value="取消" type="button" onClick="hideDialog()">
		          </td>
		        </tr>
		      </tbody>
		    </table>
	  </form>
	</div>  	
  </div>

  
  

</body>
</html>